package com.gtw.modules.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/** 普通用户
 * Created by wuye on 2018/1/17.
 */
@RestController
@RequestMapping("/sys/apiuser")
public class SysApiUserController {

    @Autowired
    private TbUserService tbUserService;

    /**
     * 查询列表
     * @param params
     * @returnapi
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:apiuser:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = tbUserService.queryPageList(pageUtil,query);
        return R.ok().put("page", page);
    }

    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sys:apiuser:info")
    public R info(@PathVariable("userId") Long userId){
        TbUser user = tbUserService.foundById(userId);
        //获取用户所属的角色列表
        return R.ok().put("user", user);
    }


    /**
     * 修改用户
     * @param tbUser
     * @return
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:apiuser:update")
    public R save(@RequestBody TbUser tbUser){
        tbUserService.update(tbUser);
        return R.ok();
    }


    /**
     * 删除用户
     * @return
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:apiuser:delete")
    public R delete(@RequestBody Long[] userIds){
        tbUserService.deleteBatch(userIds);
        return R.ok();
    }
}
